/**
 * 实用工具类型定义
 */

// 使所有属性可选
export type Partial<T> = {
  [P in keyof T]?: T[P]
}

// 使所有属性必需
export type Required<T> = {
  [P in keyof T]-?: T[P]
}

// 选择特定属性
export type Pick<T, K extends keyof T> = {
  [P in K]: T[P]
}

// 排除特定属性
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>

// 深度只读
export type DeepReadonly<T> = {
  readonly [P in keyof T]: T[P] extends object ? DeepReadonly<T[P]> : T[P]
}

// 深度可选
export type DeepPartial<T> = {
  [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P]
}

// 非空类型
export type NonNullable<T> = T extends null | undefined ? never : T

// 数组元素类型
export type ArrayElement<T> = T extends (infer U)[] ? U : never

// 函数参数类型
export type Parameters<T extends (...args: any) => any> = T extends (...args: infer P) => any ? P : never

// 函数返回类型
export type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any

// Promise 解包类型
export type Awaited<T> = T extends Promise<infer U> ? U : T

// 键值对类型
export type KeyValuePair<K extends string | number | symbol, V> = {
  [P in K]: V
}

// 字符串字面量联合类型
export type StringLiteral<T> = T extends string ? (string extends T ? never : T) : never

// 数字字面量联合类型
export type NumberLiteral<T> = T extends number ? (number extends T ? never : T) : never

// 条件类型工具
export type If<C extends boolean, T, F> = C extends true ? T : F

// 联合类型转交叉类型
export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never

// 获取对象的值类型
export type ValueOf<T> = T[keyof T]

// 创建可选字段的类型
export type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>

// 创建必需字段的类型
export type RequiredFields<T, K extends keyof T> = T & Required<Pick<T, K>>

// 递归键路径类型
export type KeyPath<T> = {
  [K in keyof T]: T[K] extends object 
    ? K | `${K & string}.${KeyPath<T[K]> & string}`
    : K
}[keyof T]

// 根据键路径获取值类型
export type GetByPath<T, P extends string> = P extends `${infer K}.${infer Rest}`
  ? K extends keyof T
    ? GetByPath<T[K], Rest>
    : never
  : P extends keyof T
    ? T[P]
    : never

// 分页数据类型
export type PaginatedData<T> = {
  data: T[]
  total: number
  page: number
  pageSize: number
  hasNext: boolean
  hasPrev: boolean
}

// API 响应类型
export type ApiResponse<T> = {
  success: boolean
  data?: T
  error?: string
  message?: string
  timestamp: number
}

// 事件处理器类型
export type EventHandler<T = any> = (event: T) => void | Promise<void>

// 异步函数类型
export type AsyncFunction<T extends any[] = any[], R = any> = (...args: T) => Promise<R>

// 同步函数类型
export type SyncFunction<T extends any[] = any[], R = any> = (...args: T) => R

// 可能为异步的函数类型
export type MaybeAsync<T extends any[] = any[], R = any> = SyncFunction<T, R> | AsyncFunction<T, R>